﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyTruongHoc_App
{
    public partial class QLNamHoc : Form
    {
        public const String OPT_DIEM_HK = "DiemHK";

        public QLNamHoc()
        {
            InitializeComponent();
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            refreshData();
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_QL_NAM_HOC);
            // user co quyen view
            /*if (!((pow & PConst.POW_VIEW) > 0))
            {
                MessageBox.Show(MSG.MSG_KHONG_DU_QUYEN);
                this.Close();
            }*/
            // user co quyen them
            dg_DanhSach.AllowUserToAddRows =
                tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);

            // user co quyen edit
            tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);

            for (int i = 0; i < dg_DanhSach.RowCount - 1; i++)
                for (int j = 1; j < dg_DanhSach.ColumnCount; j++)
                    dg_DanhSach[j, i].ReadOnly = !((pow & PConst.POW_EDIT) > 0);

            // user co delete
            dg_DanhSach.AllowUserToDeleteRows =
                tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void refreshData()
        {
            tbl_NamHocTableAdapter.Connection.ConnectionString = PublicLib.getStrConn();
            tbl_NamHocTableAdapter.ClearBeforeFill = true;
            this.tbl_NamHocTableAdapter.FillByFlag(this.qLTH.tbl_NamHoc, PConst.FLAG_NONE);
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            dg_DanhSach.Rows[dg_DanhSach.Rows.Count - 1].Cells[Ten.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value == null)
                return;
            if (dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value.ToString() == "") // add new
            {
                int newId = PublicLib.getMaxIdFromTbl(qLTH.tbl_NamHoc.TableName, qLTH.tbl_NamHoc.idColumn.ColumnName) + 1;
                dg_DanhSach.Rows[e.RowIndex].Cells[grc_id.Name].Value = newId;
            }

            dg_DanhSach.Rows[e.RowIndex].Cells[grc_flag.Name].Value = PConst.FLAG_NONE;
        }

        private void fillSTT()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_STT.Name].Value = (i + 1).ToString();
            }
        }

        private void dg_DanhSach_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            fillSTT();
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            dg_DanhSach.SelectedRows[0].Cells[Ten.Name].Selected = true;
            dg_DanhSach.BeginEdit(true);
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(e.Row.Cells[grc_id.Name].Value.ToString());
            }
            else
                e.Cancel = true;
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            }
        }

        private void deleteByID(String _id)
        {
            try
            {
                PublicLib.updateField(qLTH.tbl_NamHoc.TableName,
                                        qLTH.tbl_NamHoc.idColumn.ColumnName,
                                        _id,
                                        qLTH.tbl_NamHoc.flagColumn.ColumnName,
                                        PConst.FLAG_DELETE.ToString());
                refreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void tblDanhGiaBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            if ((DataRowView)((BindingSource)sender).Current == null)
                return;
            DataRow ThisDataRow = ((DataRowView)((BindingSource)sender).Current).Row;

            if (ThisDataRow.RowState == DataRowState.Modified ||
                ThisDataRow.RowState == DataRowState.Added)
            {
                if (ThisDataRow.RowState == DataRowState.Added)
                {
                    // them 2 hoc ky
                    MyDataSet.QLTHTableAdapters.tbl_HocKyTableAdapter hkAdap =
                        new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_HocKyTableAdapter();
                    hkAdap.Connection.ConnectionString = PublicLib.getStrConn();

                    for (int i = 0; i < 2; i++)
                    {
                        int nId = PublicLib.getMaxIdFromTbl(qLTH.tbl_HocKy.TableName, qLTH.tbl_HocKy.idColumn.ColumnName) + 1;
                        DataRow nRow = qLTH.tbl_HocKy.NewRow();
                        nRow[qLTH.tbl_HocKy.idColumn] = nId;
                        nRow[qLTH.tbl_HocKy.idNamHocColumn] = ThisDataRow[qLTH.tbl_NamHoc.idColumn];
                        nRow[qLTH.tbl_HocKy.tenColumn] = "Học Kỳ " + ( i == 0 ? "I" : "II");
                        qLTH.tbl_HocKy.Rows.Add(nRow);

                        hkAdap.Update(nRow);
                    }
                }
                tbl_NamHocTableAdapter.Update(ThisDataRow);
            }
        }

        private void QLLoaiDiem_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 15;
            dg_DanhSach.Height = stts_Bottom.Top - dg_DanhSach.Top - 1;

            //pn_Button.Left = dg_DanhSach.Right - pn_Button.Width;
            //pn_Button.Top = dg_DanhSach.Bottom;
        }

        private void dg_DanhSach_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(MSG.MSG_NHAP_SO);
        }

        public static String createHocKyName()
        {
            String result = "";
            DateTime dt = DateTime.Now;
            if (dt.Month >= 6)
            {
                result = "Năm Học " + dt.Year.ToString() + " -" + (dt.Year + 1).ToString();
            }
            else
            {
                result = "Năm Học " + (dt.Year - 1).ToString() + " -" + dt.Year.ToString();
            }

            return result;
        }

        private void dg_DanhSach_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dg_DanhSach.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "")
            {
                SendKeys.Send(createHocKyName());
            }
        }

        private void dg_DanhSach_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            //SendKeys.Send("a");
            //e.Row.Cells[grc_Ten.Index].Value = createHocKyName();
        }

        private void QLNamHoc_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }

    }
}
